package com.my.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.my.R;
import com.my.util.ApiUtil;

public class NightSpotReviewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_night_spot_review);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        JSONObject nightSpot = JSONUtil.parseObj(getIntent().getStringExtra("item"));
        ((TextView) findViewById(R.id.u_name)).setText(nightSpot.getStr("userName"));
        ((TextView) findViewById(R.id.grade_class)).setText(nightSpot.getStr("gradeName") + nightSpot.getStr("className"));
        ((TextView) findViewById(R.id.date_time)).setText(nightSpot.getStr("dateTime"));
        ((TextView) findViewById(R.id.remarks)).setText(nightSpot.getStr("remarks"));
        ((Button) findViewById(R.id.ok)).setOnClickListener(v -> {
            Toast.makeText(this, "测试通过！", Toast.LENGTH_SHORT).show();
        });
        Button ok = findViewById(R.id.ok);
        Button noOk = findViewById(R.id.no_ok);
        SharedPreferences preferences = this.getSharedPreferences("LOGIN_DATA", Context.MODE_PRIVATE);
        JSONObject user = new JSONObject(preferences.getString("user", null));
        if(user.getInt("type") == 3 || nightSpot.getInt("status") != 1) {
            ok.setVisibility(View.GONE);
            noOk.setVisibility(View.GONE);
        }
        Handler handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                JSONObject rest = ((JSONObject) msg.obj);
                Toast.makeText(NightSpotReviewActivity.this, rest.getStr("msg"), Toast.LENGTH_LONG).show();
                if(rest.getInt("code") == 200) {
                    onBackPressed();
                }
            }
        };
        ok.setOnClickListener(v -> {
            updateStatus(handler, nightSpot, 2, user);
        });
        noOk.setOnClickListener(v -> {
            updateStatus(handler, nightSpot, 3, user);
        });
    }

    public void updateStatus(Handler handler, JSONObject askLeave, int status, JSONObject user) {
        new Thread(() -> {
            Message message = new Message();
            String body = HttpRequest.put(ApiUtil.HTTP_ADDRESS + "lateComeStatus?id=" + askLeave.getLong("id") + "&status=" + status + "&approvalId=" + user.getLong("id")).execute().body();
            message.obj = new JSONObject(body);
            handler.sendMessage(message);
        }).start();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == 16908332) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}